using System.Windows.Forms;
using ShadeTree.Binding.Elements;

namespace ShadeTree.Binding.Configuration
{
    public class PropertyBindingExpression : IBindingData
    {
        private readonly IScreenBinder _binder;
        private readonly Accessor _accessor;

        public PropertyBindingExpression(IScreenBinder binder, Accessor accessor)
        {
            _binder = binder;
            _accessor = accessor;
        }

        #region IBindingData Members

        IScreenBinder IBindingData.Binder
        {
            get { return _binder; }
        }

        Accessor IBindingData.Accessor
        {
            get { return _accessor; }
        }

        #endregion



        public PropertyBindingExpression ToVisibilityOf(params Control[] controls)
        {
            foreach (var control in controls)
            {
                var element = _binder.FindElementForControl(control);
                if (element == null)
                {
                    element = new ScreenElement<Control>(control);
                }

                element.BindVisibilityTo(_accessor);
                _binder.AddElement(element);
            }
            return this;
        }

        public PropertyBindingExpression ToEnabledOf(params Control[] controls)
        {
            foreach (var control in controls)
            {
                var element = _binder.FindElementForControl(control);
                if (element == null)
                {
                    element = new ScreenElement<Control>(control);
                }

                element.BindEnabledTo(_accessor);
                _binder.AddElement(element);
            }
            return this;
        }

        public TextEditingElementExpression To(TextBox textbox)
        {
            var element = new TextboxElement(_accessor, textbox);
            _binder.AddElement(element);

            return new TextEditingElementExpression(element);
        }

        public TextEditingElementExpression To(Label label)
        {
            var element = new LabelElement(_accessor, label);
            _binder.AddElement(element);

            return new TextEditingElementExpression(element);
        }

        public ListElementExpression To(ComboBox comboBox)
        {
            var element = new PicklistElement(_accessor, comboBox);
            _binder.AddElement(element);

            return new ListElementExpression(element);
        }

        public CheckboxElementExpression To(CheckBox box)
        {
            var element = new CheckboxElement(_accessor, box);
            _binder.AddElement(element);

            return new CheckboxElementExpression(element);
        }

        public RadioGroupExpression<ENUM> ToRadioButtons<ENUM>()
        {
            return new RadioGroupExpression<ENUM>(_accessor, _binder);
        }
    }
}